﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using RedemptoristRetreatCenter.DatabaseUtility;

namespace RedemptoristRetreatCenter
{
    public partial class ViewScheduledRetreats1 : System.Web.UI.Page
    {
        /// <summary>
        /// Loads onto the page a table that contains scheduled retreats.
        /// Table contents based off of an ID variable in the Querystring, 
        /// if none it loads all of the scheduled retreats.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                DataTable scheduledRetreatsTable = new DataTable();
                DataRow tableRow = null;

                scheduledRetreatsTable.Columns.Add(new DataColumn("Title", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("Description", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("Director", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("Start Date", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("Start Time", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("End Date", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("End Time", typeof(string)));
                scheduledRetreatsTable.Columns.Add(new DataColumn("Fee", typeof(string)));

                RetreatScheduleUtility scheduledRetreatUtility = new RetreatScheduleUtility();  
                List<RetreatScheduleInfo> scheduleList = null;
              
                if (Request.QueryString.Count == 0)
                {   // get all active scheduled retreats
                    scheduleList = scheduledRetreatUtility.getAllRetreatSchedules();
                }
                else
                {
                    // get active scheduled retreats matching ids of csv list of parameters
                    scheduleList = scheduledRetreatUtility.getRetreatSchedulesByScheduleList(Request.QueryString["ID"]);
                }

                foreach (RetreatScheduleInfo schedule in scheduleList)
                {
                    tableRow = scheduledRetreatsTable.NewRow();
                    tableRow["Title"] = schedule.retreatTitle;
                    tableRow["Description"] = schedule.retreatDescription;
                    tableRow["Director"] = schedule.directorName;
                    tableRow["Start Date"] = schedule.startTimestamp.ToShortDateString();
                    tableRow["Start Time"] = schedule.startTimestamp.ToShortTimeString();
                    tableRow["End Date"] = schedule.endTimestamp.ToShortDateString(); ;
                    tableRow["End Time"] = schedule.endTimestamp.ToShortTimeString();
                    tableRow["Fee"] = schedule.retreatFee;

                    scheduledRetreatsTable.Rows.Add(tableRow);

                    // this will be used on edit page so schedule can be prepopulated
                    string rowIndexScheduleIdXref = String.Format("rowIndexScheduleIdXref_{0}", scheduledRetreatsTable.Rows.Count);
                    Session[rowIndexScheduleIdXref] = schedule.scheduleId;
                }                

                ViewState["CurrentTable"] = scheduledRetreatsTable;

                if (Session["isAdmin"] != null && (bool)Session["isAdmin"] == true)
                {
                    GridView1.AutoGenerateEditButton = true;
                    GridView1.AutoGenerateSelectButton = false;
                }
                else
                {
                    GridView1.AutoGenerateEditButton = false;
                    GridView1.AutoGenerateSelectButton = true;
                }

                GridView1.DataSource = scheduledRetreatsTable;
                GridView1.DataBind();
            }
        }

        private void redirectPage()
        {
           // get schedule id from session varibles saved in load complete
            int xrefId;

            if (GridView1.SelectedIndex < 0) 
            {   // selected index < 0 when clicking select/edit on 1st row
                xrefId = 1;
            }
            else
            {
                xrefId = GridView1.SelectedIndex + 1;
            }
            string sessionVariable = String.Format("rowIndexScheduleIdXref_{0}", xrefId);
            Response.Redirect("~/RetreatSchedule.aspx?ID=" + Session[sessionVariable].ToString());

        }

        protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
        {
            redirectPage();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            redirectPage();
        }

        protected void addScheduledRetreatButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("RetreatSchedule.aspx");
        }
 
    }
}